package com.wk.data4glib.bean;

import com.wk.data4glib.tool.WkByteBitTools;

/*

CC DD  ,00 02 ,  01  , 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06, 05 05 ,10 ,55
 */
public class ControlCmdPackage {
    private byte[] START ={ (byte)0xCC , (byte)0xDD} ;
    private byte[] playLoadLength = new byte[2];
    private byte[] cmd = new byte[1];
    private byte[] ID = new byte[15] ;
    private byte[] payload ;// 长度不定
    private byte[] checkSum = new byte[1] ;
    private byte[] end ={(byte)0x55} ;

    public byte[] pack(){
        if(payload==null){
            throw new RuntimeException("数据段还未赋值... 请先赋值") ;
        }

        int size = START.length +  playLoadLength.length + cmd.length + ID.length +
                payload.length + checkSum.length + end.length;
        byte[] lastCode=  new byte[size]  ;


        /*
        src:源数组
        srcPos:源数组要复制的起始位置
        dest:目的数组
        destPos:目的数组放置的起始位置
        length:要复制的长度*/
        int index = 0 ;
        copyArray(START,0, lastCode ,index ,START.length);
        index+= START.length;

        playLoadLength = WkByteBitTools.intTo2ByteArrayHexNormal(payload.length) ;// 小端格式
        copyArray(playLoadLength,0, lastCode ,index ,playLoadLength.length);
        index+= playLoadLength.length;

        copyArray(cmd,    0, lastCode ,index ,cmd.length);
        index+= cmd.length;

        copyArray(ID,     0, lastCode ,index ,ID.length);
        index+=ID.length ;

        copyArray(payload,0, lastCode ,index ,payload.length);
        index+=payload.length ;


        byte[]checkData= WkByteBitTools.subBytes(lastCode ,0 ,lastCode.length-2);
        checkSum[0]= WkByteBitTools.getCheckSum(checkData);

        copyArray(checkSum,0, lastCode ,index ,checkSum.length);
        index+= checkSum.length;
        copyArray(end,0, lastCode ,index ,end.length);

        return lastCode ;
    }

    public void setCmd(byte[] cmd) {
        this.cmd = cmd;
    }

    public void setID(byte[] ID) {
        this.ID = ID;
    }

    public void setPayload(byte[] payload) {
        this.payload = payload;
    }


    private void copyArray(Object var0, int var1, Object var2, int var3, int var4){
        showLog("index=="+var3);
        System.arraycopy(var0,var1, var2 ,var3 ,var4);
    }
    private void showLog(String msg){
        System.out.println("msg--->"+msg);
    }
}
